package com.example.sens.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.example.sens.entity.Conclusion;
import com.example.sens.entity.Performance;
import com.example.sens.entity.User;
import com.example.sens.entity.UserQuiz;
import com.example.sens.service.UserQuizService;
import com.example.sens.utils.Response;
import com.example.sens.vo.ConclusionVo;
import com.example.sens.vo.UserQuizVo;
import com.example.sens.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/userQuiz")
public class UserQuizController {

    @Resource
    private UserQuizService userQuizService;

    /**
     * 获取分页数据
     * @param userQuizVo
     * @return
     */
    @PostMapping("/getByPage")
    public Response<Page<UserQuiz>> getByPage(@RequestBody UserQuizVo userQuizVo, HttpSession session) {
        Page<UserQuiz> quizPage = null;
        try {
            User user = (User)session.getAttribute("user");
            quizPage = userQuizService.findAll(new Page(userQuizVo.getPageNo(), userQuizVo.getPageSize()),user.getUserId(),userQuizVo.getQuizId());
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no();
        }
        return Response.yes(quizPage);
    }

    @PostMapping("/getByPageCustom")
    public Response<Page<UserQuiz>> getByPageCustom(@RequestBody UserQuizVo userQuizVo) {
        Page<UserQuiz> quizPage = null;
        try {
            quizPage = userQuizService.findAllCustom(new Page(userQuizVo.getPageNo(), userQuizVo.getPageSize()));
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no();
        }
        return Response.yes(quizPage);
    }

    /**
     * 导出Excel
     * @param param
     * @return
     */
    @RequestMapping(value = "exportExcel", method = RequestMethod.POST)
    @ResponseBody
    public Response<String> exportExcel(@RequestBody UserQuizVo param) {
        return userQuizService.exportExcel(param);
    }


    @PostMapping("/updateScore")
    public Response updateScore(@RequestBody UserQuiz userQuiz) {
        userQuiz.setId(Long.parseLong(userQuiz.getPrimaryId()));
        return userQuizService.update(userQuiz);
    }

    @PostMapping("/updateICheck")
    public Response updateICheck(@RequestBody UserQuiz userQuiz) {
        userQuiz.setId(Long.parseLong(userQuiz.getPrimaryId()));
        userQuiz.setIsCheck(true);
        return userQuizService.update(userQuiz);
    }

}
